package com.ruoyi.flowable.config;

import com.ruoyi.flowable.listener.FlowableGlobalTaskListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.request.RequestContextListener;

/**
 * @author liuyx
 * @date 2021/4/20
 * 全局监听配置 ContextRefreshedEvent在类被初始化之后触发
 */
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private FlowableGlobalTaskListener flowableGlobalTaskListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //任务完成全局监听
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //任务创建
        dispatcher.addEventListener(flowableGlobalTaskListener);

    }
    /**
     * 监听器：监听HTTP请求事件
     * 解决RequestContextHolder.getRequestAttributes()空指针问题
     * @return
     */
    @Bean
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    }
}
